<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="projectNum">
        <el-input
          v-model="queryParams.projectNum"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目类型" prop="projectType">
        <el-select v-model="queryParams.projectType" placeholder="请选择项目类型" clearable>
          <el-option
            v-for="dict in dict.type.project_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="beginTime">
        <el-date-picker clearable
                        v-model="queryParams.beginTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="finishTime">
        <el-date-picker clearable
                        v-model="queryParams.finishTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="projectStatus">
        <el-select v-model="queryParams.projectStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.project_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="ID" align="center" prop="projectId" />-->
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目编号" align="center" prop="projectNum" />
      <el-table-column label="项目类型" align="center" prop="projectType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column label="项目总款" align="center" prop="funds" />
      <el-table-column label="项目负责人" align="center" prop="functionary" />
      <el-table-column label="状态" align="center" prop="projectStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_status" :value="scope.row.projectStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="履约保证金" align="center" prop="bail" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleProject(scope.row)"
            v-hasPermi="['project:task:edit']"
          >任务管理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!--    任务管理对话框-->
    <el-dialog title="任务管理" :visible.sync="taskVisible" width="80%">
      <div class="app-container">
        <el-form :model="taskQueryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="任务主题" prop="taskTitle">
            <el-input
              v-model="taskQueryParams.taskTitle"
              placeholder="请输入任务主题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="工作类型" prop="taskType">
            <el-select v-model="taskQueryParams.taskType" placeholder="请选择工作类型" clearable>
              <el-option
                v-for="dict in dict.type.task_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTaskQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetTaskQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div style="color: #1AB394 ; font-size: 16px ;margin: 0px 0px 20px " >项目名称：{{this.form.projectName}}</div>
      <el-table v-loading="loading" :data="taskList">
        <el-table-column label="任务主题" align="center" prop="taskTitle" />
        <el-table-column label="工作类型" align="center" prop="taskType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.task_type" :value="scope.row.taskType"/>
          </template>
        </el-table-column>
        <el-table-column label="优先级" align="center" prop="taskGrade">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.project_grade" :value="scope.row.taskGrade"/>
          </template>
        </el-table-column>
        <el-table-column label="协作人员" align="center" prop="collaborator" />
        <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="finishTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:task:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleShow(scope.row)"
              v-hasPermi="['system:task:list']"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <pagination
          v-show="taskTotal>0"
          :total="taskTotal"
          :page.sync="taskQueryParams.pageNum"
          :limit.sync="taskQueryParams.pageSize"
          @pagination="getTaskList"
        />
      </div>
    </el-dialog>

    <el-dialog title="任务查看" :visible.sync="showDialogVisible" width="80%">
      <div class="app-container">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="任务主题">{{ taskForm.taskTitle }}</el-descriptions-item>
          <el-descriptions-item label="工作类型">
            <dict-tag :options="dict.type.task_type" :value="taskForm.taskType"/>
          </el-descriptions-item>
          <el-descriptions-item label="优先级">
            <dict-tag :options="dict.type.project_grade" :value="taskForm.taskGrade"/>
          </el-descriptions-item>
          <el-descriptions-item label="协作人员">{{ taskForm.collaborator }}</el-descriptions-item>
          <el-descriptions-item label="开始时间">{{ parseTime(taskForm.beginTime, '{y}-{m}-{d}') }}</el-descriptions-item>
          <el-descriptions-item label="结束时间">{{ parseTime(taskForm.finishTime, '{y}-{m}-{d}') }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ parseTime(taskForm.createTime, '{y}-{m}-{d}') }}</el-descriptions-item>
          <el-descriptions-item label="任务描述">{{ taskForm.content }}</el-descriptions-item>
          <el-descriptions-item label="备注">{{ taskForm.remark }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
    <el-dialog
      title="任务修改"
      :visible.sync="editDialogVisible"
      width="70%"
    append-to-body
    custom-class="task-edit-dialog"
    >
    <!-- 使用卡片包裹表单增加层次感 -->
    <el-card class="task-form-card">
      <el-form
        ref="taskForm"
        :model="taskForm"
        :rules="rules"
        label-width="100px"
      class="task-form"
      >
      <el-form-item label="任务主题" prop="taskTitle">
        <el-input
          v-model="taskForm.taskTitle"
          placeholder="请输入任务主题"
          size="medium"
          class="form-input"
        />
      </el-form-item>

      <el-row :gutter="20">  <!-- 使用栅格布局并排显示 -->
        <el-col :span="12">
          <el-form-item label="工作类型" prop="taskType">
            <el-select
              v-model="taskForm.taskType"
              placeholder="请选择工作类型"
              size="medium"
              class="form-select"
              disabled
            >
              <el-option
                v-for="dict in dict.type.task_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="taskGrade">
            <el-radio-group v-model="taskForm.taskGrade" class="radio-group">
              <el-radio
                v-for="dict in dict.type.project_grade"
                :key="dict.value"
                :label="dict.value"
                class="radio-item"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="协作人员" prop="collaborator">
        <el-input
          v-model="taskForm.collaborator"
          placeholder="请输入协作人员，多个用逗号分隔"
          size="medium"
          class="form-input"
        />
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开始时间" prop="beginTime">
            <el-date-picker
              clearable
              v-model="taskForm.beginTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择开始时间"
              size="medium"
              class="form-datepicker"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="finishTime">
            <el-date-picker
              clearable
              v-model="taskForm.finishTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择结束时间"
              size="medium"
              class="form-datepicker"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="详细描述" class="form-editor">
        <editor
          v-model="taskForm.content"
          :min-height="200"
          class="editor-container"
        />
      </el-form-item>

      <el-form-item label="附件" prop="accessory">
        <el-input
          v-model="taskForm.accessory"
          type="textarea"
          placeholder="请输入附件链接，多个用换行分隔"
          rows="3"
          size="medium"
          class="form-textarea"
        />
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="taskForm.remark"
          placeholder="请输入备注信息"
          size="medium"
          class="form-input"
        />
      </el-form-item>
      </el-form>
    </el-card>

    <div slot="footer" class="dialog-footer">
      <el-button
        @click="editCancel"
        class="cancel-btn"
      >
        取 消
      </el-button>
      <el-button
        type="primary"
        @click="submitForm"
        class="confirm-btn"
      >
        确 定
      </el-button>
    </div>
    </el-dialog>


  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/project/project"
import dict from "@/utils/dict";
import { listTask, getTask, delTask, addTask, updateTask,listTaskByProjectId } from "@/api/project/task"
export default {
  name: "Task",
  dicts: ['project_status', 'project_type', 'project_grade', 'task_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //任务总条数
      taskTotal: 0,
      // 项目管理表格数据
      projectList: [],
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectNum: null,
        projectType: null,
        beginTime: null,
        finishTime: null,
        projectStatus: null,
      },
      //任务查询参数
      taskQueryParams: {
          pageNum: 1,
          pageSize: 10,
          taskTitle: null,
          taskType: null,
          projectId: null
      },
      // 表单参数
      form: {},
      //任务表单参数
      taskForm: {},
      // 表单校验
      rules: {
      },
     //任务管理对话框可见性
      taskVisible: false,
      //单个任务展示框可见性
      showDialogVisible: false,
      //任务修改对话框可见性
      editDialogVisible: false,

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询项目管理列表 */
    getList() {
      this.loading = true
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows
        this.projectList.filter(item => {
          item.bail=item.bail.money
        })
        this.total = response.total
        this.loading = false
      })
    },
    //根据项目id查询任务列表
    getTaskList(projectId) {
      console.log(this.form)
      this.taskQueryParams.projectId =projectId
      console.log(this.taskQueryParams)
      listTaskByProjectId(this.taskQueryParams).then(response => {
        this.taskList = response.rows
        this.taskTotal = response.total
      })
    },
    //点击管理
    handleProject(row) {
      this.taskVisible = true
      this.reset()
      const projectId = row.projectId || this.ids
      getProject(projectId).then(response => {
        this.form = response.data
        this.getTaskList(this.form.projectId)
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectName: null,
        projectNum: null,
        projectType: null,
        address: null,
        funds: null,
        functionary: null,
        beginTime: null,
        finishTime: null,
        delivery: null,
        guarantee: null,
        introduction: null,
        projectGrade: null,
        projectStatus: null,
        contractId: null,
        invoiceName: null,
        invoiceNumber: null,
        invoiceAddress: null,
        invoiceBank: null,
        accessory: null,
        bail: {
          money: null,
          theTime: null,
          ratio: null,
          backMoney: null,
          backTime: null,
          remark: null
        },
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null

      }
      this.resetForm("form")
    },
    //任务表单重置
    resetTask() {
      this.taskForm = {
        taskId: null,
        projectId: null,
        taskTitle: null,
        taskType: null,
        taskContent: null,
        taskTime: null,
        taskStatus: null,
        taskProgress: null,
        taskRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("taskForm")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    //搜索任务按钮操作
    handleTaskQuery() {
      this.taskQueryParams.pageNum = 1
      this.getTaskList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.projectName = null
      this.queryParams.projectNum = null
      this.queryParams.projectType = null
      this.queryParams.beginTime = null
      this.queryParams.finishTime = null
      this.queryParams.projectStatus = null
      this.handleQuery()
    },
    //重置任务搜索条件
    resetTaskQuery() {
      this.taskQueryParams.taskTitle = null
      this.taskQueryParams.taskType = null
      this.getTaskList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //修改任务信息
    handleUpdate(row) {
      this.editDialogVisible = true
      getTask(row.taskId).then(response => {
        this.taskForm = response.data
      })
    },
    //查看任务信息
    handleShow(row) {
      this.showDialogVisible = true
      getTask(row.taskId).then(response => {
        this.taskForm = response.data
      })
    },
    //提交按钮
    submitForm(){
      this.$refs.taskForm.validate(valid => {
        if (valid) {
          updateTask(this.taskForm).then(response => {
            this.editCancel()
            this.getTaskList(this.form.projectId)
            this.$modal.msgSuccess("修改成功");
          })
        }
      })
    },
    editCancel(){
      this.editDialogVisible = false
    }


  }
}
</script>
<style scoped>
.dialog-container {
  padding: 10px 0;
}

.top-tabs {
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #e8e8e8;
}

.content-wrapper {
  display: flex;
  min-height: 400px; /* 固定最小高度，避免内容过短导致布局错乱 */
}

.left-menu {
  width: 180px;
  border-right: 1px solid #e8e8e8;
}

.main-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto; /* 内容过多时可滚动 */
}
.label-item {
  text-align: right;
  padding: 5px 0;
}
.value-item {
  text-align: left;
  padding: 5px 0;
}

/* 对话框样式 */
.task-edit-dialog {
  --el-dialog-padding-primary: 20px;
}

.task-edit-dialog .el-dialog__header {
  padding: 18px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.task-edit-dialog .el-dialog__title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

/* 卡片样式 */
.task-form-card {
  border: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  border-radius: 6px;
}

/* 表单样式 */
.task-form {
  padding: 15px 0 0;
}

.task-form .el-form-item {
  margin-bottom: 18px;
}

.task-form .el-form-item__label {
  font-weight: 500;
  color: #606266;
}

/* 输入框样式 */
.form-input,
.form-select,
.form-datepicker {
  width: 100%;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.form-input:focus-within,
.form-select:focus-within,
.form-datepicker:focus-within {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

/* 文本域样式 */
.form-textarea {
  width: 100%;
  border-radius: 4px;
  resize: vertical;
}

/* 富文本编辑器样式 */
.form-editor {
  margin-top: 10px;
}

.editor-container {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  transition: border-color 0.2s ease;
}

.editor-container:focus-within {
  border-color: #409eff;
}

/* 单选按钮组样式 */
.radio-group {
  display: flex;
  gap: 15px;
  align-items: center;
}

.radio-item {
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.radio-item:hover {
  background-color: #f5f7fa;
}

/* 按钮样式 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 15px 20px 20px;
  border-top: 1px solid #f0f0f0;
  margin-top: 10px;
}

.cancel-btn {
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.cancel-btn:hover {
  background-color: #f5f5f5;
}

.confirm-btn {
  padding: 8px 24px;
  border-radius: 4px;
  background-color: #409eff;
  border-color: #409eff;
  transition: all 0.2s ease;
}

.confirm-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}
</style>
